<?php
namespace Home\Controller;
use Think\Controller;
class ArticleController extends Controller {
    public function index($cid = 0,$cyear =0 ,$cmonth = 0,$pageSize = 0){
   
    	 $a = D("article");

        if($cid!=0){
             $map['category_id'] = $cid;
        }
       

        if($cyear!=0&&$cmonth!=0){
          $s_ctime  = $cyear.'/'.$cmonth.'/01';
          $e_ctime  = date('Y/m/t',strtotime($s_ctime));
          $map['create_time'] = array(array('egt',$s_ctime),array('elt',$e_ctime));

        }
        if($pageSize!=0){
          $p = getPage($a,$map,C('PageSize'));
        }
        //
        $items = $a->order('edit_time desc')->where( $map)->select(); //
    	  // $page  = $p->show();
        // $this->assign('items',$items);
        // $this->assign('page',$page);
        // $this->display();
        // echo '$items';
        $this->ajaxReturn($items);
    }
    public function view($id){

    	$a = D("article");
     // $data["read"] = $data["read"]+1;
     // $a_obj = $a->where(array('id'=>$id));
    	$data =  $a->where(array('id'=>$id))->find();
      //$a_obj = $a->where(array('id'=>$id));
      // if(cookie('read'.$id)){

      // }
      if(cookie('read'.$id)==""){
        $a->where(array('id'=>$id))->setField('read',$data[read]+1);
        //3600秒为1小时
        cookie('read'.$id,$id,3600*24);
      }
     
     // dump($a->save());
     // $this->assign($data);
      $this->ajaxReturn($data);
      //$this->display();
    }
    public function Del($id){
      if($id==0){
        $this->error('删除失败,请选择文章ID!');
      }
      $a = D("article");
      if($a->delete($id)){
        $this->success('删除成功');
      }else{
        $this->error('删除失败');
      }
         
    }
    public function save($id = ""){
    	

    	$a = D("article");
        if(IS_POST){
        	 // echo '000';
             $data=$a->create();
             //$this->ajaxReturn($data);
             // echo ($cao);
            //  return false;
              $digest = htmlspecialchars_decode($data['content']);
              $digest = trim(strip_tags( $digest));
              $data["edit_time"]=date("Y/m/d H:i:s");
              if(strlen($digest)>200){
                $digest =mb_substr($digest,0,200,'utf-8');
              }             
              $data["digest"] =$digest;
        	
        	if($id==""){ 
        	    $data["create_time"]=date("Y/m/d H:i:s");
              $id =  $a->add($data);
              $data["id"] =$id;              
        	 }else{
              $a->save($data);
              //echo $data;
        	 }
        //	$this->redirect('View',array('id' => $id), 0, '888...'); 
       
        	//dump($data);
        	
        }else{
        	$data = ($a->where(array('id'=>$id))->find());
          // echo 'po';
        }
    	//dump($data);
    	//$id = $d->add();
    	
    	//echo $d->create(); 
    	//$d
       // $c = D("category");
     // $items = $c->select();
      $this->ajaxReturn($data);
    	// echo ($cao);
    	//dump($items);
    //	$this->assign('items',$items);

     //  $this->assign($data);
      // $this->display();
    }
}